<?php
session_start();

class User_model extends CI_Model {
	public function __construct()
	{
		 parent::__construct();
		 
	}
	function validateadmin()
	{
		$this->db->where('email', $this->input->post('email'));
		$this->db->where('password', md5($this->input->post('password')));
		$query = $this->db->get('users');
		
		if($query->num_rows == 1)
		{
			$loggedin=array("logged_in"=>1);
			$update_logged_in=$this->general_model->update('users',$loggedin,'email = '."'".$this->input->post('email')."'".' and password = '."'".md5($this->input->post('password'))."'");
			$userdetails=$this->general_model->select_val('users','*','activated = 1 and email = '."'".$this->input->post('email')."'".' and password = '."'".md5($this->input->post('password'))."'");
			$user_type =$userdetails['groupid'];
			
			$this->session->set_userdata('user_type',$user_type);
			$this->session->set_userdata("status","admin");		
			$this->session->set_userdata("adminlogged",$loggedin);
			$this->session->set_userdata("admin",$userdetails);
		
			return true;
		}
		
	}
	function validateuser()
	{
		$this->db->where('email', $this->input->post('email'));
		$this->db->where('password', md5($this->input->post('password')));
		$this->db->where('groupid',3);
		$this->db->where('activated',1);
		$query = $this->db->get('users');
		
		if($query->num_rows == 1)
		{
			$loggedin=array("logged_in"=>1);
			$update_logged_in=$this->general_model->update('users',$loggedin,'email = '."'".$this->input->post('email')."'".' and password = '."'".md5($this->input->post('password'))."'");
			$userdetails=$this->general_model->select_val('users','*','email = '."'".$this->input->post('email')."'".' and password = '."'".md5($this->input->post('password'))."'");
			
            $user_type =$userdetails['groupid'];
  
			$this->session->set_userdata('user_type',$user_type);
			$_SESSION['username'] = $userdetails['firstname'];
			$_SESSION['userid'] = $userdetails['userid'];
			$this->session->set_userdata("status","user");			
			$this->session->set_userdata("userlogged",$loggedin);
			$this->session->set_userdata("user",$userdetails);
			
			return true;
		}
		
	}
	function loggedin($user)
	{
		if($user==$this->session->userdata('status'))
		{
			$loggedin=1;
		}
		else 
		{
			$loggedin=0;
		}
		return (bool) $loggedin;
	}
	
	function logout()
	{
		$user=$this->session->userdata('user');
		//$this->general_model->update('users',array('logged_in'=>0),'userid = '.$user['userid']);
		$this->session->sess_destroy();
		
	}
}